package org.jeecg.modules.compilation.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.jeecg.common.es.JeecgElasticsearchTemplate;
import org.jeecg.modules.compilation.dto.CiDocTypeDTO;
import org.jeecg.modules.compilation.dto.ScreenDTO;
import org.jeecg.modules.compilation.dto.SearchcIDocDraftDTO;
import org.jeecg.modules.compilation.dto.TypeDTO;
import org.jeecg.modules.compilation.entity.CiDocType;
import org.jeecg.modules.compilation.entity.CISysDepart;
import org.jeecg.modules.compilation.service.ICISysDepartService;
import org.jeecg.modules.compilation.service.ICiDocTypeService;
import org.jeecg.modules.compilation.mapper.CiDocTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.io.IOException;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: ci_doc_type
 * @Author: jeecg-boot
 * @Date:   2024-03-15
 * @Version: V1.0
 */
@Service
public class CiDocTypeServiceImpl extends ServiceImpl<CiDocTypeMapper, CiDocType> implements ICiDocTypeService {

    @Autowired
    private ICISysDepartService sysDepartService;
    @Autowired
    private JeecgElasticsearchTemplate jeecgElasticsearchTemplate;

    @Override
    public TypeDTO title() {
        TypeDTO objects = new TypeDTO();
        objects.setCiDocTypeList(this.list());
        objects.setCiSysDepartList(sysDepartService.list());
        return objects;
    }

    @Override
    public List<SearchcIDocDraftDTO> search(ScreenDTO titleDTO) throws IOException {
        // 获取当前日期
        LocalDate endDate = LocalDate.now();

        LocalDate beginDate = null;
        if (titleDTO.getTime().equals("本周")) {
            // 找到本周的第一天和最后一天
            beginDate = endDate.with(DayOfWeek.MONDAY);
        } else if (titleDTO.getTime().equals("本月")) {
            beginDate = endDate.withDayOfMonth(1);
        } else if (titleDTO.getTime().equals("近一个月")) {
            beginDate = endDate.minusMonths(1).withDayOfMonth(1);

        } else if (titleDTO.getTime().equals("近三个月")) {
            beginDate = endDate.minusMonths(3).withDayOfMonth(1);

        } else if (titleDTO.getTime().equals("今年")) {
            beginDate = endDate.withDayOfYear(1);

        } else if (titleDTO.getTime().equals("近一年")) {
            beginDate = endDate.minusYears(1).withDayOfYear(1);
        }


        RestHighLevelClient restHighLevelClient = jeecgElasticsearchTemplate.elasticsearchClient();
        SearchRequest request = new SearchRequest("document");
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        if (titleDTO.getDocumentTypeName() != null && titleDTO.getDocumentTypeName() != "") {
            queryBuilder.must(QueryBuilders.matchQuery("documentTypeName", titleDTO.getDocumentTypeName()));
        }
        if (titleDTO.getDepartmentName() != null && titleDTO.getDepartmentName() != "") {
            queryBuilder.must(QueryBuilders.matchQuery("departmentName", titleDTO.getDepartmentName()));
        }
        if (titleDTO.getEnableFlag() != null && titleDTO.getEnableFlag() != "") {
            queryBuilder.must(QueryBuilders.matchQuery("enableFlag", titleDTO.getEnableFlag()));
        }
        if (beginDate != null && endDate != null) {
            queryBuilder.must(QueryBuilders.rangeQuery("publishedDate").gte(beginDate.minusDays(1)).lte(endDate));
        }
        request.source().query(queryBuilder);
        List<SearchcIDocDraftDTO> searchcIDocDraftDTOS = handleResponse(restHighLevelClient.search(request, RequestOptions.DEFAULT));
        return searchcIDocDraftDTOS;
    }

    @Override
    public List<CiDocTypeDTO> queryList() {

        List<CiDocType> ciDocTypeList = this.lambdaQuery().eq(CiDocType::getEnableFlag, "1").list();
        List<CiDocTypeDTO> ciDocTypeDTOList = new LinkedList<>();
        for (CiDocType ciDocType : ciDocTypeList) {
            CiDocTypeDTO ciDocTypeDTO = new CiDocTypeDTO();
            BeanUtil.copyProperties(ciDocType,ciDocTypeDTO);
            ciDocTypeDTOList.add(ciDocTypeDTO);
        }
        return ciDocTypeDTOList;
    }

    private List<SearchcIDocDraftDTO> handleResponse(SearchResponse search) {
        SearchHits hits = search.getHits();
        long value = hits.getTotalHits().value;
        System.out.println(value + "条数据");
        System.out.println("-------------------------------------");
        SearchHit[] hits1 = hits.getHits();
        List<SearchcIDocDraftDTO> list  = new ArrayList<>();
        for (SearchHit hit : hits1) {
            String josn= hit.getSourceAsString();
            SearchcIDocDraftDTO titleDTO1 = JSON.parseObject(josn, SearchcIDocDraftDTO.class);
            list.add(titleDTO1);
        }
        return list;
    }
}